package com.itheima.appserver.service;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.itheima.appserver.interceptor.UserHandler;
import com.itheima.dubbo.api.BlackListApi;
import com.itheima.dubbo.api.QuestionApi;
import com.itheima.dubbo.api.SettingsApi;
import com.itheima.dubbo.api.UserApi;
import com.itheima.model.domain.Question;
import com.itheima.model.domain.Settings;
import com.itheima.model.domain.UserInfo;
import com.itheima.model.vo.PageResult;
import com.itheima.model.vo.SettingsVo;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Service;

import java.util.Map;

@Service
public class SettingsService {
    @DubboReference
    private SettingsApi settingsApi;
    @DubboReference
    private QuestionApi questionApi;
    @DubboReference
    private BlackListApi blackListApi;
    @DubboReference
    private UserApi userApi;

    //查询通用设置
    public SettingsVo findById() {
        //创建返回vo对象
        SettingsVo vo = new SettingsVo();
        //获取用户id
        Long userId = UserHandler.getId();
        //获取用户手机
        String phone = UserHandler.getPhone();
        //获取陌生人问题
        Question question = questionApi.findById(userId);
//        if (question == null) {
//            question.setTxt("你喜欢java吗?");
//        } else {
//            question.setTxt(question.getTxt());
//        }
//        vo.setStrangerQuestion(question.getTxt());
        String txt = question == null ? "你喜欢java吗" : question.getTxt();
        vo.setStrangerQuestion(txt);
        vo.setPhone(phone);
        Settings settings = settingsApi.findById(userId);
        if (settings != null) {
            vo.setGonggaoNotification(settings.getGonggaoNotification());
            vo.setLikeNotification(settings.getLikeNotification());
            vo.setPinglunNotification(settings.getPinglunNotification());
        }
        return vo;
    }

    public void saveQuestions(String content) {
        //获取用户id
        Long userId = UserHandler.getId();
        //获取用户的问题
        Question question = questionApi.findById(userId);
        if (question == null) {
            //不存在,就保存
            question = new Question();
            question.setTxt(content);
            question.setUserId(userId);
            questionApi.saveQustion(question);
        } else {
            //存在就更新
            question.setTxt(content);
            questionApi.update(question);
        }
    }


    public void saveSettings(Map map) {
        //获取默认值
        boolean likeNotification = (Boolean) map.get("likeNotification");
        boolean pinglunNotification = (Boolean) map.get("pinglunNotification");
        boolean gonggaoNotification = (Boolean) map.get("gonggaoNotification");

        //获取用户id
        Long id = UserHandler.getId();
        //查询用户
        Settings settings = settingsApi.findById(id);
        //判断用户
        if (settings == null) {//不存在设置默认值保存
            settings = new Settings();
            settings.setUserId(id);
            settings.setGonggaoNotification(gonggaoNotification);
            settings.setLikeNotification(likeNotification);
            settings.setPinglunNotification(pinglunNotification);
            settingsApi.saveSettings(settings);
        } else {//存在设置传递过来的开关值
            settings.setGonggaoNotification(gonggaoNotification);
            settings.setLikeNotification(likeNotification);
            settings.setPinglunNotification(pinglunNotification);
            settingsApi.updateSettings(settings);
        }
    }

    public PageResult pageBlack(int page, int size) {
        //获取用户id
        Long userId = UserHandler.getId();
        //根据分页返回userinfo的分页对象
        IPage<UserInfo> iPage = blackListApi.pageBlack(userId, page, size);
        //转为pageresult对象
        PageResult pageResult = new PageResult(page, size, (int) iPage.getTotal(), iPage.getRecords());
        //返回
        return pageResult;
    }

    public void remove(Long uid) {
        //获取用户id
        Long userId = UserHandler.getId();
        //调用api删除
        blackListApi.remove(userId, uid);
    }
}
